<?php

namespace app\common\model;

use think\Db;
use think\Model;
use think\Validate;


/**
 * 布置作业表模型
 */
class Homeworkassignment extends Model
{
    // 表名,不含前缀
    protected $name = 'homework_assignment';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
     /**
     * 关联用户表
     *
     * @return void
     */
    public function user()
    {
        return $this->belongsTo('user', 'teacher_id');
    }

    /**
     * 总数
     */
    public function dataCount($joinTable = 'user', $joinCondition = 'a.teacher_id = t.id', $joinType = 'left', $dis = [])
    {
        // 使用模型链式操作来进行动态查询
        $res = $this->alias('a')
            ->join("$joinTable t", $joinCondition, $joinType)
            ->where($dis)
            ->count();
        return $res;
    }

    public function dataList($dis = [], $field = '*', $orderField = 'id', $orderDirection = 'desc')
    {
        $res = $this->where([]);

        foreach ($dis as $field => $condition) {
            if(Validate::is($condition, 'array') && isset($condition[0]) && $condition[0] === 'in') {
                // 处理 IN 条件
                $res->whereIn($field, $condition[1]);
            } else {
                // 普通条件
                $res->where($field, $condition);
            }
        }
        $res->order($orderField, $orderDirection);
        return $res->select();
    }

    /**
     * 信息
     */
    public function dataInfo($joinTable = null, $joinCondition = null, $joinType = null, $dis = [])
    {
         $query = $this->where($dis);

        if ($joinTable && $joinCondition && $joinType) {
            $query->alias('a')
                ->join($joinTable, $joinCondition, $joinType)
                ->field('a.*, t.username as teacher_name');
        } else {
            $query->field('*');
        }

        $res = $query->find();

        return $res;
    }

    /**
     * 新增
     */
    public function dataAdd($dis)
    {
        $res = $this->save($dis);
        return $res;
    }




}
